package module
{
	import core.Global;
	
	import events.Msg_FileDownload;
	import events.Msg_GetVersionInfo;
	
	import flash.utils.Dictionary;
	
	import interfaces.IMsgListener;
	import interfaces.IVersion;
	
	public class VersionManager implements IMsgListener,IVersion
	{
		private static const VERSION:String   = "Version.swf";
		private var m_version:Dictionary;
		
		public function VersionManager()
		{
			m_version = new Dictionary(true); 
			Global.addListener( Msg_FileDownload.M_FILEDOWNLOAD,this);
			Global.loadSwf(VERSION);
		}
		
		public function msgManager(e:*=null):void
		{
			switch( e.data.msgid)
			{
				case Msg_FileDownload.M_FILEDOWNLOAD:
					onLoadedVersion(e.data);
					break;
			}
			
		}
		
		public function getVersion(url:String):Number
		{
			var ver:* = m_version[url];
			var defver:Number = Math.round(Math.random() * 100);
			return (ver == null ? (defver) : (ver));
		}
		
		private function onLoadedVersion(msg:Msg_FileDownload):void
		{
			if(msg.url.indexOf(VERSION) != -1)
			{
				var ver:Array = msg.loader.content["getVersion"]();
				var i:int = 0;
				while (i < ver.length)
				{
					m_version[ver[i][0]] = ver[i][1];
					i++;
				}
				Global.dispatchEvent(Msg_GetVersionInfo.M_GETVERSIONINFO,Msg_GetVersionInfo.createMessage());
			}
		}
	}
}